package etlkettle.demo.controller;

import ch.ethz.ssh2.StreamGobbler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.util.concurrent.Executors;

@RestController
@RequestMapping("/c")
public class XShellUtilsController {

    public static void main(String[] args)   throws IOException, InterruptedException {
        boolean isWindows = System.getProperty("os.name").toLowerCase().startsWith("windows");
        ProcessBuilder builder = new ProcessBuilder();
        if (isWindows) {
            System.out.println("isWindow");
            builder.command("cmd.exe", "/c", "dir");
        } else {
            builder.command("sh", "-c", "ls");
            System.out.println("noWindows");
        }
        /*builder.directory(new File(System.getProperty("user.home")));
        Process process = builder.start();
        StreamGobbler streamGobbler =
                new StreamGobbler(process.getInputStream(),"OUTPUT");
        Executors.newSingleThreadExecutor().submit(streamGobbler);
        int exitCode = process.waitFor();
        assert exitCode == 0;*/
        Process process = Runtime.getRuntime().exec("cmd - /k C:\\Users\\Admin\\Downloads\\pdi-ce-8.3.0.0-371\\data-integration\\Kitchen.bat -file=C:\\Users\\Admin\\Documents\\test.ktr -logfile=C:\\Users\\Admin\\Documents\\2.txt");
        process.waitFor();
        System.out.println(String.valueOf(process.exitValue()));

    }
//public static void main(String[] args) {
//    execRetCode("notepad");
//}
//    /**
//     * 执行并返回状态码
//     *
//     * @param cmd
//     * @return
//     */
//    public static int execRetCode(String cmd) {
//
//        boolean isWindows = System.getProperty("os.name").toLowerCase().startsWith("windows");
//
//        BufferedReader br = null;
//        try {
//            String prefix1 = isWindows ? "cmd" : "/bin/sh";
//            String prefix2 = isWindows ? "/c" : "-c";
//
//            // 这里务必是有命令前缀, 否则直接执行程序, 日志重定向是失败的. 例如 ping www.baidu.com > a.txt
//            Process p = Runtime.getRuntime().exec(new String[]{prefix1, prefix2, cmd});
//
//            // 0 表示正常
//            int returnCode = p.waitFor();
//
//            if (returnCode != 0) {
//                String charsetName = isWindows ? "GB2312" : "UTF-8";
//
//                // 这里务必是有命令前缀, 否则直接执行程序, 日志重定向是失败的. 例如 ping www.baidu.com > a.txt
//                br = new BufferedReader(new InputStreamReader(p.getErrorStream(), charsetName));
//                String line;
//                StringBuilder sb = new StringBuilder();
//                while ((line = br.readLine()) != null) {
//                    sb.append(line + "\n");
//                }
////                log.error("shell 执行失败: {}", sb);
//            }
//
////            log.info("cmd = {}, returnCode = {}", cmd, returnCode);
//            return returnCode;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return -1;
//        } finally {
//            if (br != null) {
//                try {
//                    br.close();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//    }

}
